package com.jhgsys.internal.base.service.impl;

import com.jhgsys.internal.base.entity.HazardExamineUser;
import com.jhgsys.internal.base.mapper.HazardExamineUserMapper;
import com.jhgsys.internal.base.service.IHazardExamineUserService;
import com.jhgsys.internal.common.entity.FebsConstant;
import com.jhgsys.internal.common.entity.QueryRequest;
import com.jhgsys.internal.common.utils.FebsUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.annotation.Propagation;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.baomidou.mybatisplus.core.toolkit.StringPool;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

/**
 *  Service实现
 *
 * @author admin
 * @date 2024-09-04 13:23:28
 */
@Service
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class HazardExamineUserServiceImpl extends ServiceImpl<HazardExamineUserMapper, HazardExamineUser> implements IHazardExamineUserService {

    /**
     * 查询（分页）
     * @author admin
     * @date 2024-09-04 13:23:28
     * @param request QueryRequest
     * @param hazardExamineUser hazardExamineUser
     * @return IPage<HazardExamineUser>
    */
    @Override
    public IPage<HazardExamineUser> findHazardExamineUserPage(QueryRequest request, HazardExamineUser hazardExamineUser) {
        LambdaQueryWrapper<HazardExamineUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(HazardExamineUser::getDeletedFlg, 0);
        // TODO 设置查询条件

        Page<HazardExamineUser> page = new Page<>(request.getPageNum(), request.getPageSize());
        FebsUtil.pageWrapper(page, request);
        return this.page(page, queryWrapper);
    }

    /**
     * 查询（所有）
     * @author admin
     * @date 2024-09-04 13:23:28
     * @param hazardExamineUser hazardExamineUser
     * @return List<HazardExamineUser>
     */
    @Override
    public List<HazardExamineUser> findHazardExamineUserList(HazardExamineUser hazardExamineUser) {
	    LambdaQueryWrapper<HazardExamineUser> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(HazardExamineUser::getDeletedFlg, FebsConstant.VALID);
		// TODO 设置查询条件
        //检查项id
        if(StringUtils.isNotBlank(hazardExamineUser.getExamineObjectId())){
            queryWrapper.eq(HazardExamineUser::getExamineObjectId, hazardExamineUser.getExamineObjectId());
        }
		return this.baseMapper.selectList(queryWrapper);
    }

    /**
    * 根据ID查询
    * @author admin
    * @date 2024-09-04 13:23:28
    * @param id
    * @return HazardExamineUser
    */
    @Override
    public HazardExamineUser findHazardExamineUserById(String id){
        return this.baseMapper.selectById(id);
    }

    /**
     * 新增
     * @author admin
     * @date 2024-09-04 13:23:28
     * @param hazardExamineUser hazardExamineUser
     */
    @Override
    @Transactional
    public void createHazardExamineUser(HazardExamineUser hazardExamineUser) {
        hazardExamineUser.setCreatedTime(new Date());
        this.save(hazardExamineUser);
    }

    /**
     * 修改
     * @author admin
     * @date 2024-09-04 13:23:28
     * @param hazardExamineUser hazardExamineUser
     */
    @Override
    @Transactional
    public void updateHazardExamineUser(HazardExamineUser hazardExamineUser) {
        hazardExamineUser.setUpdatedTime(new Date());
        this.saveOrUpdate(hazardExamineUser);
    }

    /**
     * 逻辑删除
     * @author admin
     * @date 2024-09-04 13:23:28
     * @param hazardExamineUser hazardExamineUser
     * @param hazardExamineUserIds hazardExamineUserIds
     */
    @Override
    @Transactional
    public void deleteHazardExamineUser(HazardExamineUser hazardExamineUser,String hazardExamineUserIds) {
        List<String> list = Arrays.asList(hazardExamineUserIds.split(StringPool.COMMA));
        this.baseMapper.update(hazardExamineUser,new LambdaQueryWrapper<HazardExamineUser>().in(HazardExamineUser::getExamineUserId,list));
	}

    /**
    * 物理删除
    * @author admin
    * @date 2024-09-04 13:23:28
    * @param hazardExamineUserIds hazardExamineUserIds
    */
    @Override
    @Transactional
    public void physicallyDeleteHazardExamineUser(String hazardExamineUserIds) {
        List<String> list = Arrays.asList(hazardExamineUserIds.split(StringPool.COMMA));
        LambdaQueryWrapper<HazardExamineUser> wapper = new LambdaQueryWrapper<>();
        wapper.in(HazardExamineUser::getExamineUserId,list);
        this.remove(wapper);
    }

    /**
    * 根据检查id删除
    * @author admin
    * @date 2024-09-04 13:23:28
    * @param hazardExamineIds hazardExamineIds
    */
    @Override
    @Transactional
    public void deleteByHazardExamineIds(String hazardExamineIds) {
        List<String> list = Arrays.asList(hazardExamineIds.split(StringPool.COMMA));
        LambdaQueryWrapper<HazardExamineUser> wapper = new LambdaQueryWrapper<>();
        wapper.in(HazardExamineUser::getExamineId,list);
        this.remove(wapper);
    }
}
